/*
 * @Author: panghu chenjh@datamargin.com
 * @Date: 2024-04-17 17:32:32
 * @LastEditors: panghu chenjh@datamargin.com
 * @LastEditTime: 2024-04-26 17:24:00
 * @FilePath: \zheye\src\utils\request.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import axios from 'axios'
import store from '@/store'

const request = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '/daili' : 'http://api.vikingship.xyz'
})

request.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  store.commit('SET_LOADING', true)
  store.commit('SET_ERROR', { status: false })
  return config
})

request.interceptors.response.use(response => {
  store.commit('SET_LOADING', false)
  return response
}, err => {
  console.log(err.response)
  const { error } = err.response.data
  store.commit('SET_ERROR', { status: true, message: error })
  store.commit('SET_LOADING', false)
  return Promise.reject(error)
})

export default request
